import { Component } from '@nestjs/common';
import * as elasticsearch from 'elasticsearch';
import axios from 'axios';
// import { Observable } from 'rxjs/Rx';

// const baseUrl = 'http://localhost:9200';
const baseUrl = 'http://119.29.116.62:9200';
/**
 * elastic数据访问
 *
 * @export
 * @class ElasticDto
 */
@Component()
export class ElasticDto {
  private client: elasticsearch.Client;
  constructor() {
    // 创建elasticsearch的客户端进行数据分析，查询
    this.client = new elasticsearch.Client({
      host: baseUrl,
      log: 'info',
    });
  }

  /**
   * 通过开始位置和size来得到数据，用于主页的[最新]展示
   *
   * @param {number} from
   * @param {number} size 默认6
   * @memberof ElasticDto
   */
  getLastestByPage(from: number = 0, size: number = 6) {
    return axios.get(baseUrl + '/cookbooks/_search', {
      data: {
        _source: ['title', 'pic_url', 'tags', 'date', 'order', 'fans', 'watch'],
        size,
        from,
        sort: [
          {
            date: {
              order: 'desc',
            },
          },
        ],
      },
    });
  }

  /**
   * 根据文档id得到文档
   *
   * @param {string} id
   * @returns
   * @memberof ElasticDto
   */
  getOneById(id: string) {
    return this.client.get({
      index: 'cookbooks',
      type: 'cookbook',
      id,
    });
  }

  /**
   * 通过【热度】来展示菜谱数据
   *
   * @param {number} from
   * @param {number} size 默认6
   * @memberof ElasticDto
   */
  getHotestByPage(from: number = 0, size: number = 6) {
    return axios.get(baseUrl + '/cookbooks/_search', {
      data: {
        _source: ['title', 'pic_url', 'tags', 'date', 'order', 'fans', 'watch'],
        size,
        from,
        sort: [
          {
            fans: {
              order: 'desc',
            },
          },
        ],
      },
    });
  }

  /**
   * 通过【热度】获得首页轮播图
   * @memberof ElasticDto
   */
  getCoursels() {
    return axios.get(baseUrl + '/cookbooks/_search', {
      data: {
        _source: ['title', 'pic_url'],
        size: 5,
        from: 0,
        sort: [
          {
            fans: {
              order: 'desc',
            },
          },
        ],
      },
    });
  }

  /**
   * 根据分类获得
   *
   * @param {string} category
   * @param {number} [from=0]
   * @param {number} [size=6]
   * @memberof ElasticDto
   */
  getByCategory(
    type: string,
    sortType: string = 'date',
    from: number = 0,
    size: number = 6,
  ) {
    const sort =
      sortType === 'date'
        ? [
            {
              date: {
                order: 'desc',
              },
            },
          ]
        : [
            {
              fans: {
                order: 'desc',
              },
            },
          ];
    return this.client.search({
      index: 'cookbooks',
      body: {
        _source: ['title', 'pic_url', 'tags', 'date', 'order', 'fans', 'watch'],
        query: {
          match: {
            title: type,
          },
        },
        size,
        from,
        sort,
      },
    });
  }

  /**
   * 根据关键词进行搜索
   *
   * @param {string} keyword
   * @returns {*}
   * @memberof ElasticDto
   */
  searchByKeyword(keyword: string): any {
    return this.client.search({
      index: 'cookbooks',
      body: {
        _source: ['title', 'pic_url', 'date'],
        suggest: {
          cook_suggests: {
            text: keyword,
            completion: {
              field: 'suggest',
              fuzzy: {
                fuzziness: 1,
              },
              size: 5,
            },
          },
        },
      },
    });
  }

  /**
   * lucene DSL搜索
   *
   * @param {string} query
   * @returns
   * @memberof ElasticDto
   */
  specialSearch(query: string) {
    return this.client.search({
      index: 'cookbooks',
      body: {
        _source: ['title', 'pic_url', 'date'],
        size: 10,
        query: {
          query_string: {
            fields: ['title', 'tags'],
            query,
            analyzer: 'ik_max_word',
          },
        },
      },
    });
  }

  /**
   * 代理es请求
   *
   * @param {*} data
   * @returns
   * @memberof ElasticDto
   */
  proxyQuery(data: any) {
    return axios.post(baseUrl + '/cookbooks/_search', data);
  }

  wordAnalyze(text: string) {
    return axios.post(baseUrl + '/_analyze', {
      analyzer: 'ik_smart',
      text,
    });
  }

  /**
   * 测试【连接】
   *
   * @memberof ElasticDto
   */
  ping() {
    this.client.ping({});
  }
}
